﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.OleDb;
using System.Security.Cryptography;
using System.IO;


namespace CTManagerSoln
{
    public class Commons
    {
       //OleDbConnection conn = new OleDbConnection();
        public static OleDbConnection conn=new OleDbConnection();
        public static string ConnectionString;
        public static bool isAuthen { get; set; }
        public static string UsernameLogin { get; set; }
        public static bool isKiemDuyet { get; set; }
        public static bool isAdmin { get; set; }


        public static System.Data.OleDb.OleDbConnection DBConnect
        {
            get 
            {
                //string path = System.Reflection.Assembly.GetExecutingAssembly().Location;
                ////string path1 = System.Reflection.Assembly.GetExecutingAssembly().CodeBase;
                //var directory = System.IO.Path.GetDirectoryName(path);
                ////D:\Trainings\C#\OneDrive\QLChungTu\CTManagerSoln\CTManagerSoln\CTManagerSoln\CTManagerSoln\bin\Debug
                //directory += directory + "\\CTManage.accdb";
                //string strconn = string.Format("Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Persist Security Info=False", directory);
                //conn = new OleDbConnection(strconn);

                string path = Environment.CurrentDirectory;
                path = path + "\\CTManage.accdb";
                //conn = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0; Data Source=" + path);

                //conn = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0; Data Source=D:\DB\CTManage.accdb");
                //conn.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0; Data Source=D:\DB\CTManage.accdb";
                conn.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0; Data Source="+path;
                return conn;
            }
            set 
            {
                conn = value;
            }
        }
        //public static byte[] GetHashKey(string hashKey)
        //{
        //    // Initialize
        //    UTF8Encoding encoder = new UTF8Encoding();
        //    // Get the salt
        //    string salt = !string.IsNullOrEmpty(Salt) ? Salt : "I am a nice little salt";
        //    byte[] saltBytes = encoder.GetBytes(salt);
        //    // Setup the hasher
        //    Rfc2898DeriveBytes rfc = new Rfc2898DeriveBytes(hashKey, saltBytes);
        //    // Return the key
        //    return rfc.GetBytes(16);
        //}
        public static string Encrypt(byte[] key, string dataToEncrypt)
        {
            // Initialize
            AesManaged encryptor = new AesManaged();
            // Set the key
            encryptor.Key = key;
            encryptor.IV = key;
            // create a memory stream
            using (MemoryStream encryptionStream = new MemoryStream())
            {
                // Create the crypto stream
                using (CryptoStream encrypt = new CryptoStream(encryptionStream, encryptor.CreateEncryptor(), CryptoStreamMode.Write))
                {
                    // Encrypt
                    byte[] utfD1 = UTF8Encoding.UTF8.GetBytes(dataToEncrypt);
                    encrypt.Write(utfD1, 0, utfD1.Length);
                    encrypt.FlushFinalBlock();
                    encrypt.Close();
                    // Return the encrypted data
                    return Convert.ToBase64String(encryptionStream.ToArray());
                }
            }
        }
        public static string Decrypt(byte[] key, string encryptedString)
        {
            // Initialize
            AesManaged decryptor = new AesManaged();
            byte[] encryptedData = Convert.FromBase64String(encryptedString);
            // Set the key
            decryptor.Key = key;
            decryptor.IV = key;
            // create a memory stream
            using (MemoryStream decryptionStream = new MemoryStream())
            {
                // Create the crypto stream
                using (CryptoStream decrypt = new CryptoStream(decryptionStream, decryptor.CreateDecryptor(), CryptoStreamMode.Write))
                {
                    // Encrypt
                    decrypt.Write(encryptedData, 0, encryptedData.Length);
                    decrypt.Flush();
                    decrypt.Close();
                    // Return the unencrypted data
                    byte[] decryptedData = decryptionStream.ToArray();
                    return UTF8Encoding.UTF8.GetString(decryptedData, 0, decryptedData.Length);
                }
            }
        }



        
    }
}
